import { ref } from 'vue';
import workshopRequest from '@/api/md/workshop/workshop.js';

export const workshopList = ref([]);  // 全部数据
export const pagedData = ref([]);  // 分页后的数据
export const total = ref(0);  // 总数据条数
export const pageSize = ref(10);  // 每页条数
export const currentPage = ref(1);  // 当前页码

// 查询条件
export const searchWorkshop = ref({
    workshopName: '',  // 分类名称
    workshopCode: ''   // 分类编码
});

// 分页处理函数
export function handlePageChange(page) {
    currentPage.value = page;
    paginateData();
}

// 根据当前页码和每页条数分页
export function paginateData() {
    const start = (currentPage.value - 1) * pageSize.value;
    const end = start + pageSize.value;
    pagedData.value = workshopList.value.slice(start, end);
    console.log(pagedData.value)
}

// 获取数据
export function fetchWorkshopList(searchWorkshop) {
    workshopRequest.loadWorkshopList(searchWorkshop, function (data) {
        workshopList.value = data.data;
        total.value = workshopList.value.length;  // 设置总条数
        paginateData();  // 设置分页后的数据
    });
}

// 搜索重置
export function resetSearch() {
    searchWorkshop.value.workshopCode = '';
    searchWorkshop.value.workshopName = '';
    fetchWorkshopList(searchWorkshop.value);
}

